﻿using System.Collections.Generic;
using CTEM.Models;
using CTEM.Controllers.DAO;
using System;
using CTEM.Controllers.Global;

namespace CTEM.Controllers.BUS
{
    public class SupplierBUS
    {
        private readonly DT_SupplierDAO _suppDAO = new DT_SupplierDAO();

        #region Business Layer for Supplier Entity
        /// <summary>
        /// 
        /// </summary>
        /// <param name="subconDTO"></param>
        /// <param name="errorMessage"></param>
        /// <param name="fieldName"></param>
        /// <returns></returns>
        public bool ValidateSupplier(DT_SupplierDTO subconDTO, out string errorMessage, out string fieldName)
        {
            subconDTO.SupplierName = Global.Global.TitleCase(subconDTO.SupplierName);
            subconDTO.SupplierName = Global.Global.RemoveRedundantSpace(subconDTO.SupplierName);
            if (subconDTO.SupplierName.Equals(Constants.EMPTY_TEXT))
            {
                errorMessage = "Bạn Chưa Nhập Tên Cung Cấp";
                fieldName = "SupplierName";
                return false;
            }
            else if (subconDTO.SupplierName.Length > 50)
            {
                errorMessage = "Tên Nhà Cung Cấp Không Được Dài Quá 50 Ký ";
                fieldName = "SupplierName";
                return false;
            }
            else if (subconDTO.ManagerID == 0)
            {
                errorMessage = "Bạn Chưa Chọn Người Quản Lý";
                fieldName = "ManagerID";
                return false;
            }
            
            else if (subconDTO.SupplierName.Equals(Constants.EMPTY_TEXT))
            {
                List<DT_SupplierDTO> listsubcon = LoadSupplier(false);
                foreach (var item in listsubcon)
                {
                    if (item.SupplierName.Equals(subconDTO.SupplierName))
                    {
                        errorMessage = "Tên Nhà Cung Cấp Bạn Vừa Nhập Đã Tồn Tại";
                        fieldName = "SupplierName";
                        return false;
                    }
                }
            }
            errorMessage = "";
            fieldName = "";
            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="subconDTO"></param>
        /// <returns></returns>
        public long CreateSupplier(DT_SupplierDTO subconDTO)
        {
            subconDTO.SupplierName = Global.Global.TitleCase(subconDTO.SupplierName);
            subconDTO.SupplierName = Global.Global.RemoveRedundantSpace(subconDTO.SupplierName);
            return _suppDAO.CreateSupplier(subconDTO);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="subconID"></param>
        /// <returns></returns>
        public bool DeleteSupplier(long subconID)
        {
            return _suppDAO.DeleteSupplier(subconID);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="isactive"></param>
        /// <returns></returns>
        internal List<DT_SupplierDTO> LoadSupplier(bool isactive)
        {
            return _suppDAO.LoadAllSuppliers(isactive);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="subconDTO"></param>
        /// <returns></returns>
        internal bool UpdateSupplier(DT_SupplierDTO subconDTO)
        {
            subconDTO.UpdatedBy = Global.Global.CurrentUser.Username;
            subconDTO.LastUpdated = DateTime.Today;
            subconDTO.SupplierName = Global.Global.TitleCase(subconDTO.SupplierName);
            subconDTO.SupplierName = Global.Global.RemoveRedundantSpace(subconDTO.SupplierName);
            return _suppDAO.UpdateSupplier(subconDTO);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="subconID"></param>
        /// <returns></returns>
        internal DT_SupplierDTO GetSupplierByID(long subconID)
        {
            return _suppDAO.GetSupplierByID(subconID);
        }
        #endregion
    }
}
